using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Framework.Managers;
using Yoozoo.Gta.Common;
using Yoozoo.Libs;

namespace Gameplay.PVE.TurnBase
{
    public enum ECounterType
    {
        None,
        Counter,
        BeCountered,
    }
    
    public class UnitData : IClass
    {
        void IClass.OnReset()
        {
            
        }

        private BattleUnit unit;
        public int id;
        public int team;
        public int column;
        public int row;
        public TbsHeroConfig config;
        public Vector3 birthPosition;
        public Vector3 birthForward;
        public int index;
        public bool isDead;
        public int modelId;
        public float moveSpeed = 5f * PveUtils.globalRatio;
        public float damageRatio = 1f;
        public Vector3 colliderCenter = new Vector3(0, PveUtils.globalRatio * 0.8f, 0);
        public Vector3 colliderSize = new Vector3(PveUtils.globalRatio * 1.2f, PveUtils.globalRatio * 2 * 0.8f, PveUtils.globalRatio * 1.2f);
        
        
        public List<int> controlList = new List<int>();
        
        public float critChance;
        public float critTimes;


        public float AttackValue;
        public float DefenceValue;

        public bool isInControl = false;
        
        public Vector3 velocity;

        public Vector3 targetPosition;

        public bool isFloating;

        public Vector3 floatingPosition;

        public bool isInBirthPosition = false;
        
        public float TotalHp;
        public float CurrentHp
        {
            get { return currentHp; }
            set
            {
                currentHp = value;
                if (currentHp > TotalHp)
                {
                    currentHp = TotalHp;
                }

                if (currentHp <= 0)
                {
                    currentHp = 0;
                    unit.Dead();
                    TurnBaseManager.Instance.UnitDead(team,id);
                }
                unit.Transmit(ETransmitType.UpdateHp);
                if (team == 0)
                {
                    ResidentHandler.Inst.GetFunction("pveManager.TbsUpdateHeroHp").Action(id,currentHp / TotalHp);
                }
            }
        }
        private float currentHp;

        public float TotalMp;

        public float CurrentMp
        {
            get { return currentMp; }
            set
            {
                currentMp = value;
                if (currentMp > TotalMp)
                {
                    currentMp = TotalMp;
                }
                if (team == 0)
                {
                    ResidentHandler.Inst.GetFunction("pveManager.TbsSetIsCanSkill").Action(id,CanUseSkill());
                    ResidentHandler.Inst.GetFunction("pveManager.TbsUpdateHeroMp").Action(id,currentMp / TotalMp);
                }
            }
        }
        private float currentMp;

        private int restMoveCount = 1;

        public int RestMoveCount
        {
            get { return restMoveCount; }
            set
            {
                restMoveCount = value;
                ResidentHandler.Inst.GetFunction("pveManager.UpdateRestMoveCount").Action(id,restMoveCount);
            }
        }
        
        public bool IsInAction;
        public bool IsCasting;
        public float enemyCastCd = 1f;
        public bool IsEnemyCastCd;//用于限制敌方放技能的间隔

        public bool isJoining = false;

        public void Init(BattleUnit unit)
        {
            this.unit = unit;
           
            int heroId = index;
            if (team == 2)
            {
                heroId = index + 10;
            }

            damageRatio = team == 0 ? 5 : 0.5f;
            config = PveUtils.GetTbsHeroConfig(heroId);
            /*if (index == 0)
            {
                config = PveUtils.GetTbsHeroConfig(6);
            }*/
            //modelId = 0;//config.modelId;
            modelId = config.modelId;//team == 0 ? 21 : 22;
            TotalHp = 100;
            CurrentHp = 100;
            TotalMp = 100;
            CurrentMp = 0;
            column = index % 3;
            row = index / 3;
            AttackValue = 10;
            DefenceValue = 10;
            critChance = config.critChance / 10000f;
            critTimes = config.critTimes / 10000f;
        }
        
        public Vector3 position { get; private set; }

        public void SetPosition(Vector3 position)
        {
            this.position = position;
        }

        public Vector3 forward { get; private set; }

        public void SetForward(Vector3 forward)
        {
            if (forward == Vector3.zero)
            {
                return;
            }
            this.forward = forward.normalized;
        }

        public bool CanUseSkill()
        {
            return currentMp == TotalMp;
        }

        public int GetTargetTeam()
        {
            if (team == 0)
            {
                return 2;
            }
            else
            {
                return 0;
            }
        }

        public bool IsCanMove()
        {
            return restMoveCount > 0;
        }
        
        public ECounterType GetCounterType(int targetProperty)
        {
            int myProperty = config.property;
            if (myProperty == targetProperty)
            {
                return ECounterType.None;
            }

            if (myProperty - targetProperty == 1 || myProperty % 3 - targetProperty %3 == 1)
            {
                return ECounterType.Counter;
            }

            return ECounterType.BeCountered;
        }
    }
}